package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.system.entity.SysMail;
import cn.e4j.modular.system.service.SysMailService;
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/mail")
public class SysMailCotnroller extends BaseController {
    @Autowired
    SysMailService sysMailService;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:mail:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/mail.html";
    }

    /**
     * 查询邮件参数列表
     */
    @RequestMapping("/getMailList")
    @RequiresPermissions("sys:mail:init")
    @ResponseBody
    public ResponseData getMailList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysMailService.getMailList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加邮件参数
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:mail:add")
    @ResponseBody
    public ResponseData add(SysMail mail) {
        // 已经启用的邮件参数
        if(mail.getMAILUD001().equals("Y")){
            QueryWrapper<SysMail> mailTQueryWrapper = new QueryWrapper<>();
            mailTQueryWrapper.eq("MAILUD001", "Y");
            int count = sysMailService.count(mailTQueryWrapper);
            if(count > 0) {
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        "存在已启用的邮件参数");
            }
        }
        try {
            mail.setMAIL_PAW(CryptoUtils.encrypt(mail.getMAIL_PAW()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        sysMailService.add(mail);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:mail:edit")
    @ResponseBody
    public ResponseData edit(SysMail mail) {
        if(mail.getMAILUD001().equals("Y") && sysMailService.existedTheParams(mail.getID())){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    "存在已启用的邮件参数");
        }
        try {
            sysMailService.edit(mail);
        } catch (Exception e) {
            return new ErrorResponseData(BizExceptionEnum.MAIL_ENCRYPT_FAILED.getCode(),
                    BizExceptionEnum.MAIL_ENCRYPT_FAILED.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:mail:del")
    @ResponseBody
    public ResponseData del(SysMail mail) {
        sysMailService.delete(mail);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("sys:mail:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysMail> mailList = inDto.getReqList(SysMail.class);
        sysMailService.deleteBatch(mailList);
        return SUCCESS_TIP;
    }
}
